<template>
  <div class="dialog">
    <el-dialog width="45%" :title="title" :visible="true" :close-on-click-modal="false" :before-close="close">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="字典名称：" prop="name">
          <el-col :span="12">
            <el-input v-model="form.name" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
        <el-form-item label="状态：" prop="status">
          <el-col :span="12">
            <el-select v-model="form.status" placeholder="请选择" clearable>
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="分组：" prop="dictClass">
          <el-col :span="12">
            <el-select v-model="form.dictClass" placeholder="请选择" clearable>
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="图片：">
          <el-col :span="12">
            <UploadImg :file-list="form.imgUrl" @change="uploadHandle($event, 'imgUrl')" />
          </el-col>
        </el-form-item>
        <el-form-item label="备注：">
          <el-col :span="12">
            <el-input v-model="form.remark" placeholder="请输入" clearable />
          </el-col>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" :loading="loading" @click="confirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import UploadImg from '@/components/UploadFile/index.vue'
import { ACCOUNT_STATUS } from '@/utils/enum'
export default {
  name: 'AddEnum',
  components: {
    UploadImg
  },
  props: {
    type: {
      type: String,
      default: 'add'
    },
    formDetail: {
      type: Object,
      default: () => ({})
    }
  },

  data() {
    return {
      loading: false,
      statusOptions: ACCOUNT_STATUS,
      form: {
        name: '',
        status: '',
        dictClass: '',
        imgUrl: '',
        remark: ''
      },
      rules: {
        name: [{ required: true, message: '请输入字典名称', trigger: ['blur'] }],
        status: [{ required: true, message: '请选择状态', trigger: ['change'] }],
        dictClass: [{ required: true, message: '请选择分组', trigger: ['change'] }]
      }
    }
  },

  computed: {
    title() {
      if (this.type === 'add') {
        return '新增字典'
      } else {
        return '编辑字典'
      }
    }
  },

  created() {
    const { name, status, dictClass, imgUrl, remark } = this.formDetail
    this.form = {
      name,
      status,
      dictClass,
      imgUrl,
      remark
    }
  },

  methods: {

    // 取消
    close() {
      this.$emit('close')
    },

    // 确定
    confirm() {
      if (this.type === 'add') {
        this.addHandle()
      } else {
        this.editHandle()
      }
    },

    // 新增
    addHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form
          }
          console.log(data)
          // addUser(data).then(() => {
          //   this.$message.success('新增成功')
          //   this.loading = false
          //   this.$emit('confirm')
          //   this.close()
          // }).catch(() => {
          //   this.loading = false
          // })
        } else {
          return false
        }
      })
    },

    // 编辑
    editHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          const data = {
            ...this.form,
            id: this.formDetail.id
          }
          console.log(data)
          // editUser(data).then(() => {
          //   this.$message.success('编辑成功')
          //   this.loading = false
          //   this.$emit('confirm')
          //   this.close()
          // }).catch(() => {
          //   this.loading = false
          // })
        } else {
          return false
        }
      })
    },

    // 上传图片处理
    uploadHandle(payload, name) {
      this.form[name] = payload.fileList[0]?.url
    }

  }

}
</script>

<style lang="scss" scoped>
.el-select{
  width: 100%;
}
</style>
